**Setting up country-year party id
gen rparty_id_b=pf_party_id
**Addressing issue where the ruling party is the winner of previous election
replace rparty_id_b=rparty_id_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_id_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_id_b=rparty_id_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_id_b[_n-1]~=rparty_id_b





**Setting up country-year party vote share
gen rparty_vote_b=v2pavote
**Addressing issue where the ruling party is the winner of previous election
replace rparty_vote_b=rparty_vote_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_vote_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_vote_b=rparty_vote_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_vote_b[_n-1]~=rparty_vote_b



**Setting up country-year party seat share
gen rparty_seat_b=v2paseatshare
**Addressing issue where the ruling party is the winner of previous election
replace rparty_seat_b=rparty_seat_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_seat_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_seat_b=rparty_seat_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_seat_b[_n-1]~=rparty_seat_b


***Total strength
gen share_b=rparty_seat_b
replace share_b=rparty_vote_b if share==. 




**Setting up country-year local org strength
gen rparty_lorg_b=v2paactcom
**Addressing issue where the ruling party is the winner of previous election
replace rparty_lorg_b=rparty_lorg_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_lorg_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_lorg_b=rparty_lorg_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_lorg_b[_n-1]~=rparty_lorg_b



**Setting up country-year personalization strength
gen rparty_pers_b=v2paind
**Addressing issue where the ruling party is the winner of previous election
replace rparty_pers_b=rparty_pers_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_pers_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_pers_b=rparty_pers_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_pers_b[_n-1]~=rparty_pers_b



**Setting up country-year cohesion
gen rparty_cohes_b=v2padisa
**Addressing issue where the ruling party is the winner of previous election
replace rparty_cohes_b=rparty_cohes_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_cohes_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_cohes_b=rparty_cohes_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_cohes_b[_n-1]~=rparty_cohes_b


**Setting up country-year cohesion ordinal
gen rparty_cohes_b_ord=v2padisa_ord
**Addressing issue where the ruling party is the winner of previous election
replace rparty_cohes_b_ord=rparty_cohes_b_ord[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_cohes_b_ord, replace
sort cowcode year





***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_cohes_b_ord=rparty_cohes_b_ord[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_cohes_b_ord[_n-1]~=rparty_cohes_b_ord


gen defections_b=1 if rparty_cohes_b_ord==0 | rparty_cohes_b_ord==1
replace defections_b=0 if rparty_cohes_b_ord>1 & rparty_cohes_b_ord<5

 


**Setting up country-year clientelism
gen rparty_client_b=v2paclient
**Addressing issue where the ruling party is the winner of previous election
replace rparty_client_b=rparty_client_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_client_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_client_b=rparty_client_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_client_b[_n-1]~=rparty_client_b







**Setting up country-year local offices
gen rparty_off_b=v2palocoff
**Addressing issue where the ruling party is the winner of previous election
replace rparty_off_b=rparty_off_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_off_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_off_b=rparty_off_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_off_b[_n-1]~=rparty_off_b




**Setting up country-year social org. ties
gen rparty_soc_b=v2pasoctie
**Addressing issue where the ruling party is the winner of previous election
replace rparty_soc_b=rparty_soc_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_soc_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_soc_b=rparty_soc_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_soc_b[_n-1]~=rparty_soc_b




**Setting up country-year candidate nominations 
gen rparty_nom_b=v2panom
**Addressing issue where the ruling party is the winner of previous election
replace rparty_nom_b=rparty_nom_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_nom_b, replace
sort cowcode year





***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_nom_b=rparty_nom_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_nom_b[_n-1]~=rparty_nom_b


**Binary 
gen leader_nominates_b=1 if rparty_nom_b==0
replace leader_nominates_b=0 if rparty_nom_b~=0 & rparty_nom_b~=.



**Setting up country-year right/left
gen rparty_rl_b=v2pariglef
**Addressing issue where the ruling party is the winner of previous election
replace rparty_rl_b=rparty_rl_b[_n-1] if b_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_rl_b, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_rl_b=rparty_rl_b[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_rl_b[_n-1]~=rparty_rl_b



**Other
replace b_auth=1 if cowcode==365 & (year<1992 & year>1971)
replace b_auth=0 if year==2008 & cowcode==771


gen marx_b= marx
replace marx_b=0 if marx_b==. & b_auth==1 
replace marx_b=1 if cowcode==110 & year==1992
replace marx_b=1 if cowcode==101 & year>2000 & year<2005
replace marx_b=1 if cowcode==290 & year==1989
replace marx_b=1 if cowcode==310 & year==1990
replace marx_b=1 if cowcode==339 & year==1992
replace marx_b=1 if cowcode==355 & year==1990
replace marx_b=1 if cowcode==360 & year==1990
replace marx_b=1 if cowcode==365 & year>1971 & year<1992
replace marx_b=1 if cowcode==404 & year>1993 & year<1998
replace marx_b=1 if cowcode==439 & year>1971 & year<2016
replace marx_b=1 if cowcode==541 & year>1993 & year<2004
replace marx_b=1 if cowcode==712 & year>1989 & year<1992
replace marx_b=1 if cowcode==402 & year==1991
replace marx_b=1 if cowcode==433 & year==2000
replace marx_b=1 if cowcode==438 & year==1984
replace marx_b=1 if cowcode==520 & year==1991
replace marx_b=1 if cowcode==700 & year==1992
replace marx_b=1 if cowcode==775 & year==1988

**Reverse
gen rparty_nom_rev_b=-1*rparty_nom_b



***Social Org Index
gen org_index_b=rparty_lorg_b+rparty_soc_b+rparty_off_b

**Elite indiex
gen elite_index_b=rparty_nom_rev_b+rparty_pers_b







